<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="90px">
			<el-form-item label="优惠券名称" prop="name">
				<el-input v-model="queryParams.name" placeholder="请输入优惠券名称" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="折扣" prop="discount">
				<el-input v-model="queryParams.discount" placeholder="请输入折扣" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="领取状态" prop="status">
				<el-select v-model="queryParams.status" placeholder="请选择领取状态" clearable>
					<el-option v-for="dict in dict.type.coupon_status" :key="dict.value" :label="dict.label"
						:value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="优惠券类型" prop="status">
				<el-select v-model="queryParams.type" placeholder="请选择优惠券类型" clearable>
					<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
					v-hasPermi="['business:couponReal:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
					v-hasPermi="['business:couponReal:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete" v-hasPermi="['business:couponReal:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
					v-hasPermi="['business:couponReal:export']">导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="couponRealList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="id" align="center" prop="id" width="50" />
			<el-table-column label="发放者" align="center" prop="shop.name" width="80" />
			</el-table-column>
			<el-table-column label="优惠券名称" align="center" prop="name" />
			<el-table-column label="优惠券类型" align="center" prop="type" />
			<el-table-column label="折扣" align="center" prop="discount" width="50">
				<template slot-scope="scope">
					<span v-if="scope.row.discount!=null">{{scope.row.discount}}</span>
					<span v-else>暂无</span>
				</template>
			</el-table-column>
			<el-table-column label="触发金额 " align="center" prop="triggerMoney">
				<template slot-scope="scope">
					<span v-if="scope.row.triggerMoney!=null">{{scope.row.triggerMoney}}</span>
					<span v-else>暂无</span>
				</template>
			</el-table-column>
			<el-table-column label="优惠金额" width="100" align="center" prop="preferentialMoney">
				<template slot-scope="scope">
					<span v-if="scope.row.preferentialMoney!=null">{{scope.row.preferentialMoney}}</span>
					<span v-else>暂无</span>
				</template>
			</el-table-column>
			<el-table-column label="开始时间" align="center" prop="start" width="100">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.start, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="结束时间" align="center" prop="end" width="100">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.end, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="投放数量" align="center" prop="num" width="100" />
			<el-table-column label="领取数量" align="center" prop="size" width="100" />
			<el-table-column label="领取状态" align="center" prop="status" width="80">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.coupon_status" :value="scope.row.status" />
				</template>
			</el-table-column>
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
						v-hasPermi="['business:couponReal:edit']">修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
						v-hasPermi="['business:couponReal:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加或修改平台优惠券对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="100px">
				<el-form-item label="优惠券名称" prop="name">
					<el-input v-model="form.name" placeholder="请输入优惠券名称" />
				</el-form-item>
				<el-form-item label="优惠券类型" prop="type">
					<el-radio :disabled="readOnly" v-model="form.type" label="满减券"
						@input="couponTypeShow">满减券</el-radio>
					<el-radio :disabled="readOnly" v-model="form.type" label="折扣券"
						@input="couponTypeShow">折扣券</el-radio>
				</el-form-item>

				<el-form-item label="折扣" prop="discount" :hidden="couponTypeZheShow">
					<el-input v-model.number="form.discount" placeholder="请输入折扣" />
				</el-form-item>
				<el-form-item label="触发金额 " prop="triggerMoney" :hidden="couponTypeManShow">
					<el-input v-model.number="form.triggerMoney" placeholder="请输入触发金额 " />
				</el-form-item>
				<el-form-item label="优惠金额" prop="preferentialMoney" :hidden="couponTypeManShow">
					<el-input v-model.number="form.preferentialMoney" placeholder="请输入优惠金额" />
				</el-form-item>
				<el-form-item label="投放开始时间" prop="start">
					<el-date-picker clearable v-model="form.start" type="date" value-format="yyyy-MM-dd"
						placeholder="请选择投放开始时间">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="投放结束时间" prop="end">
					<el-date-picker clearable v-model="form.end" type="date" value-format="yyyy-MM-dd"
						placeholder="请选择投放结束时间">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="投放数量" prop="num">
					<el-input v-model.number="form.num" placeholder="请输入投放数量" />
				</el-form-item>
				<el-form-item label="领取数量" prop="size">
					<el-input v-model.number="form.size" placeholder="请输入领取数量" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listCouponReal,
		getCouponReal,
		delCouponReal,
		addCouponReal,
		updateCouponReal
	} from "@/api/business/couponReal";

	export default {
		name: "CouponReal",
		dicts: ['coupon_status'],
		data() {
			var checkName = (rule, value, callback) => {
				if (!value) {
					return callback(new Error('名称不能为空'));
				} else if (value.size > 15) {
					return callback(new Error('名称不能超过15个字符'));
				}
				callback();
			};
			var checkDiscount = (rule, value, callback) => {
				if (this.form.type == "满减券") {
					callback();
				}
				if (!value) {
					return callback(new Error('折扣不能为空'));
				} else if (!Number.isInteger(value)) {
					return callback(new Error('请输入数字值'));
				} else if (value < 1 || value > 10) {
					return callback(new Error('请输1-10之间的数字'));
				}
				callback();
			};
			var checkTriggerMoney = (rule, value, callback) => {
				if (this.form.type == "折扣券") {
					callback();
				}
				if (!value) {
					return callback(new Error('金额不能为空'));
				} else if (!Number.isInteger(value)) {
					return callback(new Error('请输入数字值'));
				} else if (value <= 0 || value > 999999) {
					return callback(new Error('请输合理金额'));
				}
				callback();
			};
			var checkPreferentialMoney = (rule, value, callback) => {
				if (this.form.type == "折扣券") {
					callback();
				}
				if (!value) {
					return callback(new Error('金额不能为空'));
				} else if (!Number.isInteger(value)) {
					return callback(new Error('请输入数字值'));
				} else if (value <= 0 || value > 999999) {
					return callback(new Error('请输合理金额'));
				} else if (value > this.form.triggerMoney) {
					return callback(new Error('金额不能大于满减金额'));
				}
				callback();
			};
			var checkStart = (rule, value, callback) => {
				if (!value) {
					return callback(new Error('开始时间不能为空'));
				}
				callback();
			};
			var checkEnd = (rule, value, callback) => {
				var start = new Date(this.form.start).getTime();
				var end = new Date(value).getTime();
				if (!value) {
					return callback(new Error('结束时间不能为空'));
				} else if (start > end) {
					return callback(new Error('结束时间不能早于开始时间'));
				}
				callback();
			};
			var checkNum = (rule, value, callback) => {
				if (!value && value != 0) {
					return callback(new Error('数量不能为空'));
				} else if (!Number.isInteger(value)) {
					return callback(new Error('请输入数字值'));
				} else if (value < 0 || value > 100000) {
					return callback(new Error('请输1-100000之间的数字'));
				}
				callback();
			};
			var checkSize = (rule, value, callback) => {
				if (!value) {
					return callback(new Error('数量不能为空'));
				} else if (!Number.isInteger(value)) {
					return callback(new Error('请输入数字值'));
				} else if (value < 1 || value > 100000) {
					return callback(new Error('请输1-100000之间的数字'));
				}
				callback();
			};
			return {
				options: [{
					value: '满减券',
					label: '满减券'
				}, {
					value: '折扣券',
					label: '折扣券'
				}],
				//规则
				rules: {
					name: [{
						validator: checkName,
						trigger: 'blur'
					}],
					discount: [{
						validator: checkDiscount,
						trigger: 'blur'
					}],
					triggerMoney: [{
						validator: checkTriggerMoney,
						trigger: 'blur'
					}],
					preferentialMoney: [{
						validator: checkPreferentialMoney,
						trigger: 'blur'
					}],
					start: [{
						validator: checkStart,
						trigger: 'blur'
					}],
					end: [{
						validator: checkEnd,
						trigger: 'blur'
					}],
					num: [{
						validator: checkNum,
						trigger: 'blur'
					}],
					size: [{
						validator: checkSize,
						trigger: 'blur'
					}]
				},
				//根据优惠券类型展示
				couponTypeZheShow: true,
				couponTypeManShow: false,
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 平台优惠券表格数据
				couponRealList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 时候可以领取时间范围
				daterangeStart: [],
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					source: null,
					mid: null,
					name: null,
					type: null,
					discount: null,
					status: null
				},
				// 表单参数
				form: {},
				readOnly: false
			};
		},
		created() {
			this.getList();
		},
		methods: {
			// 新增优惠券类型发生改变的阿时候
			couponTypeShow() {
				if (this.form.type == "满减券") {
					this.couponTypeManShow = false;
					this.couponTypeZheShow = true;
					this.form.discount = "";
				} else {
					this.couponTypeManShow = true;
					this.couponTypeZheShow = false;
					this.form.preferentialMoney = "";
					this.form.triggerMoney = "";
				}
			},
			/** 查询平台优惠券列表 */
			getList() {
				this.loading = true;
				this.queryParams.source = 1;
				this.queryParams.params = {};
				if (null != this.daterangeStart || '' != this.daterangeStart) {
					this.queryParams.params["beginStart"] = this.daterangeStart[0];
					this.queryParams.params["endStart"] = this.daterangeStart[1];
				}
				listCouponReal(this.queryParams).then(response => {
					this.couponRealList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					id: null,
					source: null,
					mid: null,
					name: null,
					type: null,
					discount: null,
					triggerMoney: null,
					preferentialMoney: null,
					start: null,
					end: null,
					num: null,
					size: null,
					status: null
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.daterangeStart = [];
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.readOnly = false;
				this.form.source = 1;
				this.form.type = "满减券";
				this.couponTypeManShow = false;
				this.couponTypeZheShow = true;
				this.open = true;
				this.title = "添加平台优惠券";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				this.readOnly = true;
				const id = row.id || this.ids
				getCouponReal(id).then(response => {
					this.form = response.data;
					this.couponTypeShow();
					this.open = true;
					this.title = "修改平台优惠券";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.id != null) {
							updateCouponReal(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addCouponReal(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const ids = row.id || this.ids;
				this.$modal.confirm('是否确认删除平台优惠券编号为"' + ids + '"的数据项？').then(function() {
					return delCouponReal(ids);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('business/couponReal/export', {
					...this.queryParams
				}, `couponReal_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>